import {reqcartlist,reqcartdelete,reqcartedit} from '../../request/api'
import { getUserInfo } from "./Login"
import { Toast } from "antd-mobile"

//状态
let isRequst = true;
const initState ={
    shoplist:[],
    allCheck:false,
    isshow:false
}

//actionType
const TYPES={
    SHOP_CHANGE_SHOPLIST:"SHOP_CHANGE_SHOPLIST",
    SHOP_CHANGE_ALLCHECK:"SHOP_CHANGE_ALLCHECK",
    SHOP_CHANGE_MONEY:"SHOP_CHANGE_MONEY",
    SHOP_CHANGE_ISSHOW:"SHOP_CHANGE_ISSHOW"
}

//修改state reducer
const reducer = (state=initState,action)=>{
    switch(action.type){
        case TYPES.SHOP_CHANGE_SHOPLIST:
            return {
                ...state,
                shoplist:action.shoplist
            }
        case TYPES.SHOP_CHANGE_ALLCHECK:
            return {
                ...state,
                allCheck:action.allCheck
            }
        case TYPES.SHOP_CHANGE_MONEY:
            return {
                ...state,
                money:action.money
            }
        case TYPES.SHOP_CHANGE_ISSHOW:
            return {
                ...state,
                isshow:action.isshow
            }
        default:
            return state;
    }
}

//逻辑和异步actions actionCreators
export const actions={
    //修改shoplist
    changeShoplist:shoplist=>({type:TYPES.SHOP_CHANGE_SHOPLIST,shoplist}),

    requestSL:()=>(dispatch,getState)=>{
        reqcartlist({uid:getUserInfo(getState()).uid}).then(d=>{
            if(d.data.code===200){
                let oldSl = getSl(getState())
                let SL = d.data.list?d.data.list:[]
                let newSl = SL[1].content

                newSl.forEach(item=>{
                    let obj = oldSl.find(i=>i.id ===item.id)
                    item.checked = obj ? obj.checked : false
                })
                dispatch(actions.changeShoplist(newSl))
                dispatch(actions.changeAllCheck(newSl.length>0?newSl.every(item=>item.checked):false))
                isRequst = true;
            }
        })
    },
    changeAllCheck:allCheck=>({type:TYPES.SHOP_CHANGE_ALLCHECK,allCheck}),
    //删除
    del:(id)=>(dispatch,getState)=>{
        reqcartdelete({id,type:2}).then(d=>{
            if(d.data.code === 200){
                dispatch(actions.requestSL())
            }
        })
    },
    //8.数量+
    add: (id) => (dispatch, getState) => {
        reqcartedit({ id, type: 2 }).then(res => {
            if (res.data.code === 200) {
                //刷list 
                dispatch(actions.requestSL())
            }
        })
    },
    // 9.数量- 
    sub: (id, num) => (dispatch, getState) => {
        //如果当前的num=1,不能减少了
        if (num <= 1) {
            Toast.show({
                content: "宝贝数量不能再少了"
            })
            return;
        }
        // 节流：isRequest=true,一旦发请求，立刻变false,再也无法发请求，知道list重新请回来才可以再次发请求。
        if (isRequst) {
            isRequst = false;
            //自减请求
            reqcartedit({ id, type: 1 }).then(res => {
                if (res.data.code === 200) {
                    //刷list 
                    dispatch(actions.requestSL())
                }
            })
        }
    },
    changeOne:(e,index)=>(dispatch,getState)=>{
        let shoplist = getSl(getState())
        shoplist[index].checked=e.target.checked;

        dispatch(actions.changeAllCheck(shoplist.every(item=>item.checked)))
        dispatch(actions.changeShoplist([...shoplist]))
    },
    changeAll:(e,index)=>(dispatch,getState)=>{
        let newStatus = e.target.checked;
        //修改全选
        dispatch(actions.changeAllCheck(newStatus));
        //修改数组
        let arr = getSl(getState());
        arr.forEach(item => {
            item.checked = newStatus;
        })
        dispatch(actions.changeShoplist([...arr]))
    },
    changeIsshow:isshow=>({type:TYPES.SHOP_CHANGE_ISSHOW,isshow}),
    xiugai:()=>(dispatch,getState)=>{
        let isshow=getIsshow(getState())
        isshow=!isshow
        dispatch(actions.changeIsshow(isshow))
    }

}

//导出数据 Selectors
export const getSl = state =>state.Shop.shoplist
export const getAllCheck = state =>state.Shop.allCheck
export const getIsshow = state =>state.Shop.isshow 
export const getMoney = stete =>{
    let arr = getSl(stete)
    let sum = 0
    arr.forEach(item=>{
        if(item.checked){
            sum+=item.price * item.num
        }
    })
    return sum
}
//导出reduer
export default reducer;